package com.wang.transaction.product.service.impl;

import com.wang.transaction.product.domain.ProductInfo;
import com.wang.transaction.product.mapper.ProductInfoDao;
import com.wang.transaction.product.service.MPProductInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 商品 服务实现类
 * </p>
 *
 * @author wly
 * @since 2018-07-16
 */
@Service
public class ProductInfoServiceImpl extends ServiceImpl<ProductInfoDao, ProductInfo> implements MPProductInfoService {

    @Autowired
    private MPProductInfoService mpProductInfoService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void serviceA() throws Exception {
        System.out.println("ServiceA");
        ProductInfo productInfo = new ProductInfo();
        productInfo.setProductName("serviceA测试Required");
        productInfo.setAmount(100);
        productInfo.setVersion(0L);
        baseMapper.insert(productInfo);
        try{
            mpProductInfoService.serviceB();
        }catch (Exception e){
            e.printStackTrace();
        }
        System.out.println("A end");
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void serviceB() throws Exception {
        ProductInfo productInfo = new ProductInfo();
        productInfo.setProductName("serviceB测试Required");
        productInfo.setAmount(100);
        productInfo.setVersion(0L);
        baseMapper.insert(productInfo);
        System.out.println("ServiceB");
        throw new Exception();
    }

    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    @Override
    public void serviceC() throws Exception {
        ProductInfo productInfo = new ProductInfo();
        productInfo.setProductName("serviceC测试Required");
        productInfo.setAmount(100);
        productInfo.setVersion(0L);
        baseMapper.insert(productInfo);
        System.out.println("ServiceC");
//        try{
            mpProductInfoService.serviceD();
//        }catch (Exception e){
//            e.printStackTrace();
//        }
        System.out.println("C end");
        throw new Exception();
    }

    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
    @Override
    public void serviceD() throws Exception {
        ProductInfo productInfo = new ProductInfo();
        productInfo.setProductName("serviceD测试Required_NEW");
        productInfo.setAmount(100);
        productInfo.setVersion(0L);
        baseMapper.insert(productInfo);
        System.out.println("ServiceD");
//        throw new Exception();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void serviceE() throws Exception {
        ProductInfo productInfo = new ProductInfo();
        productInfo.setProductName("serviceE测试Nest");
        productInfo.setAmount(100);
        productInfo.setVersion(0L);
        baseMapper.insert(productInfo);
        System.out.println("ServiceE");
        try{
            mpProductInfoService.serviceF();
        }catch (Exception e){
            e.printStackTrace();
        }
        System.out.println("E end");

    }

    @Transactional(rollbackFor = Exception.class, propagation = Propagation.NESTED)
    @Override
    public void serviceF() throws Exception {
        ProductInfo productInfo = new ProductInfo();
        productInfo.setProductName("serviceF测试Nest");
        productInfo.setAmount(100);
        productInfo.setVersion(0L);
        baseMapper.insert(productInfo);
        System.out.println("ServiceF");
        throw new Exception();
    }
}
